﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pasta.Medio.Dialogs
{
	public partial class DialogBase : Form
	{
		private bool _autoSave;

		#region Lifetime                - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		public DialogBase() : this( true )
		{
			// for designer purposes
		}

		/// <summary>
		/// Dialog constructor
		/// </summary>
		/// <param name="services"></param>
		/// <param name="autoSave"></param>
		public DialogBase( bool autoSave = true )
		{
			InitializeComponent();
			sideImage.Image = null;
			_autoSave = autoSave;
			buttonPanel.Height = 38;

			if( _autoSave)
			{
				// cache the pending changes
			}
		}

		protected virtual void RefreshView()
		{
		}

		/// <summary>
		/// Close the dialog with the desired result
		/// </summary>
		/// <param name="result"></param>
		public void Close( DialogResult result )
		{
			DialogResult = result;
			base.Close();
		}

		/// <summary>
		/// Reset the Dataset from the _cachedDataSet
		/// </summary>
		protected void DiscardChanges()
		{
			// discard changes made in the dialog interaction
		}

		#endregion Lifetime             - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		#region Properties              - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		/// <summary>
		/// Whether the form should allow resizing
		/// </summary>
		public bool Sizable
		{
			get { return FormBorderStyle == FormBorderStyle.Sizable; }
			set { FormBorderStyle = (value ? FormBorderStyle.Sizable : FormBorderStyle.FixedDialog); }
		}

		/// <summary>
		/// Dialog title
		/// </summary>
		public string Title
		{
			get { return title.Text; }
			set { title.Text = value; }
		}

		/// <summary>
		/// Text in status pane
		/// </summary>
		public string StatusText
		{
			set
			{
				//ulStatus.Text = value;
				//ulStatus.Visible = !String.IsNullOrEmpty( value );
			}
		}

		/// <summary>
		/// Logo to display on dialog
		/// </summary>
		public Image Logo
		{
			get { return logo.BackgroundImage; }
			set { logo.BackgroundImage = value; }
		}

		/// <summary>
		/// Image to display on left side of dialog
		/// </summary>
		public Image SideImage
		{
			get { return sideImage.BackgroundImage; }
			set
			{
				sideImage.BackgroundImage = value;
				bool visible = (sideImage.BackgroundImage != null);
				sideImage.Visible = visible;
			}
		}

		/// <summary>
		/// Flag to indicate muting control events
		/// </summary>
		protected bool Mute { get; set; }

		#endregion Properties           - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		#region Event Handling          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		private void DialogBase_Load( object sender, EventArgs e )
		{
			RefreshView();
		}

		private void DialogBase_FormClosing( object sender, FormClosingEventArgs e )
		{
			if( DialogResult != DialogResult.OK )
			{
				DiscardChanges();
			}
			else if( _autoSave )
			{
				// save the data
			}
		}

		private void buttonPanel_Resize( object sender, EventArgs e )
		{
			int left = buttonPanel.Width - cancel.Width - 15;
			cancel.Location = new Point( left, (buttonPanel.Height - cancel.Height) / 2 );
			left -= ok.Width - 15;
			ok.Location = new Point( left, (buttonPanel.Height - ok.Height) / 2 );
			//ulStatus.Location = new Point( 10, (buttonPanel.Height - ulStatus.Height) / 2 );
		}

		#endregion Event Handling       - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	}
}
